package com.jomoo.common.utils;

import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 基础类，继承actionsupport
 * 
 * @author haosc
 * @date 2018/06/07
 */
public class BaseAction extends ActionSupport implements ServletRequestAware,
		ServletResponseAware, SessionAware, ServletContextAware
{
	private static final long serialVersionUID = 1L;
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected Map<String, Object> session;
	protected ServletContext application;

	Log log = LogFactory.getLog(BaseAction.class);

	@Override
	public void setServletRequest(HttpServletRequest request)
	{
		this.request = request;
	}

	@Override
	public void setServletResponse(HttpServletResponse response)
	{
		this.response = response;
	}

	@Override
	public void setSession(Map<String, Object> session)
	{
		this.session = session;
	}

	@Override
	public void setServletContext(ServletContext context)
	{
		this.application = context;
	}

	/**
	* @param object    设定文件
	* @deprecated 统一处理json返回页面值
     **/
	public void returnData(Object object)
	{
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out;
		try
		{
			out = response.getWriter();
			out.println(object);
			out.flush();
			out.close();
		} 
		catch (Exception e)
		{
			// 开发时调试使用
			e.printStackTrace();
			
			// 存入日志，解决问题使用
			log.error("action返回数据有误"+e.getMessage());
		}
	}
}
